package com.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.portal.admin.base.entity.BaseAction;
import com.portal.admin.base.entity.ResultInfo;
import com.portal.admin.message.dto.MessageDto;
import com.portal.admin.message.entity.SysMessage;
import com.portal.admin.message.service.SysMessageService;
import com.portal.admin.user.entity.SysUser;
import com.portal.admin.user.service.SysUserService;
import com.portal.admin.utils.admin.ObjectUtils;
import com.portal.admin.utils.admin.StringUtils;
import com.portal.admin.utils.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
*【消息信息】页面接口
*/
@RestController
@RequestMapping("/api/message")
public class ApiMessageController extends BaseAction {

    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private SysUserService sysUserService;

    /**
    * 【消息信息】根据条件分页查询
    * @param page
    * @param sysMessage
    * @return
    */
    @RequestMapping("/getPage")
    public ResultInfo getPage(Page<SysMessage> page, SysMessage sysMessage) {
        IPage<SysMessage> iPage = sysMessageService.getPage(page, sysMessage);
        return ResultInfo.ok("获取分页成功", iPage);
    }


    /**
    * 【消息信息】根据条件查询
    * @param sysMessage
    * @return
    */
    @RequestMapping("/getList")
    public ResultInfo getList(SysMessage sysMessage) {
        List<SysMessage> list = sysMessageService.getList(sysMessage);
        List<MessageDto> data = new ArrayList<>();
        for (SysMessage message : list) {
            MessageDto messageDto = new MessageDto();
            SysUser sysUser = sysUserService.get(message.getUserId());
            if(ObjectUtils.isNotNull(sysUser)){
                messageDto.setPhoto(sysUser.getPhoto());
                messageDto.setLoginName(sysUser.getLoginName());
                messageDto.setUserName(sysUser.getUserName());
                messageDto.setEmail(sysUser.getEmail());
                messageDto.setId(message.getId());
                messageDto.setPid(message.getPid());
                messageDto.setCreateDate(message.getCreateDate());
                messageDto.setUserId(message.getUserId());
                messageDto.setMsgType(message.getMsgType());
                messageDto.setContent(message.getContent());
                data.add(messageDto);
            }
        }
        //查询出pid为空的且类型为留言的
        List<MessageDto> chatMainList = data.stream().filter(message -> 1==(message.getMsgType())).filter(message -> null==(message.getPid())).collect(Collectors.toList());
        for (MessageDto messageDto : chatMainList) {
            List<MessageDto> children = data.stream().filter(chat -> messageDto.getId().equals(chat.getPid())).collect(Collectors.toList());
            messageDto.setChildren(children);
        }
        return ResultInfo.ok("获取列表成功", chatMainList);
    }

    /**
    * 【消息信息】根据id查询
    * @param id
    * @return
    */
    @RequestMapping("/get")
    public ResultInfo get(String id) {
        SysMessage sysMessage = sysMessageService.get(id);
        return ResultInfo.ok("获取对象成功", sysMessage);
    }

    /**
    * 【消息信息】提交(新增或修改)
    * @param sysMessage
    * @return
    */
    @RequestMapping("/sub")
    public ResultInfo insert(SysMessage sysMessage) {
        String currentUserId = SessionUtils.getCurrentUserId();
        if(StringUtils.isEmpty(currentUserId)){
            return ResultInfo.error("后天检测到您还没有登录,不能操作!");
        }
        if (StringUtils.isEmpty(sysMessage.getId())) { //新增
            sysMessage.setMsgType(1);//留言板
            sysMessage.setUserId(SessionUtils.getCurrentUserId());
            sysMessageService.insert(sysMessage);
        }
        return ResultInfo.ok("提交成功!");
    }

    /**
    * 【消息信息】删除
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    public ResultInfo delete(String id) {
        sysMessageService.delete(id);
        SysMessage message = new SysMessage();
        message.setPid(id);
        List<SysMessage> list = sysMessageService.getList(message);
        for (SysMessage sysMessage : list) {
            sysMessageService.delete(sysMessage.getId());
        }
        return ResultInfo.ok("删除成功!");
    }

}



